package org.nanotek.web;

import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.codec.binary.Base64;
import org.nanotek.beans.ArtistName;
import org.nanotek.dao.mapper.ArtistMapper;
import org.nanotek.mail.MailSender;
import org.nanotek.rpt.JasperReportBase;
import org.nanotek.service.ArtistService;

@SuppressWarnings("serial")
@ManagedBean
@ViewScoped
public class ArtistBean extends WebBean<ArtistName> {

	@Inject private ArtistName artist; 
	private Long id;
	private List<?> artists;
	private Integer artistStartList;
	
	@ManagedProperty(name="artistCreditBean", value = "#{artistCreditBean}")
	private ArtistCreditBean artistCreditBean;

	@ManagedProperty(name="releaseBean", value = "#{releaseBean}")
	private ReleaseBean releaseBean;
	
	@EJB
	private ArtistService artistService;
	
	
	private String artistNameValue;

	public ArtistBean()
	{ 
	}

	public <T extends ArtistName> void findArtist()
	{ 
		artist = new ArtistMapper<T>().apply(artistService.findArtistById(id));
		System.out.println(artist.toString() + " " + artist.getClass().getName());
	}

	public void findAll()
	{ 
		artists = artistService.loadArtistList(artistStartList, 2000);
	}

	public void findArtistByName()
	{ 
		artists = artistService.findArtistByName(artistNameValue);
	}
	
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public ArtistName getArtistName() {
		return artist;
	}

	public void setArtistName(ArtistName artistName) {
		this.artist = artistName;
	}

	public ArtistService getArtistService() {
		return artistService;
	}

	public void setArtistService(ArtistService artistService) {
		this.artistService = artistService;
	}

	public List<?> getArtists() {
		return artists;
	}

	public void setArtists(List<?> artists) {
		this.artists = artists;
	}

	public Integer getArtistStartList() {
		return artistStartList;
	}

	public void setArtistStartList(Integer artistStartList) {
		this.artistStartList = artistStartList;
	}

	public void exportSearchResult()
	{ 
		@SuppressWarnings("unused")
		List<Map<String,?>> results = artistService.prepareReportResult(artists);
		try{ 
			JasperReportBase reportBase = new JasperReportBase();
			byte[] report = new Base64().decode(reportBase.exportReport(results));  

			MailSender sender = new MailSender();
			List mailTo = new ArrayList<>();
			mailTo.add("jcanova@brq.com");
			sender.sendMail(mailTo, "This is the Subject", "This is the body" , report);
			/*return "artist-view?faces-redirect=true"; */
			getFacesResponse().setContentType("application/pdf");
			getFacesResponse().setHeader("Content-Disposition", "attachment; filename=\"relatorio.pdf\"");
			getFacesResponse().setContentLength(report.length);

			getReponseOutputStream().write(report );
			getReponseOutputStream().flush();
			getReponseOutputStream().close();
		}catch (Exception ex)
		{
			ex.printStackTrace();
		}

		getFacesContext().responseComplete();
	}

	protected FacesContext getFacesContext() 
	{ 
		return FacesContext.getCurrentInstance();
	}

	protected HttpServletResponse getFacesResponse() {

		return (HttpServletResponse)FacesContext.getCurrentInstance()
				.getExternalContext().getResponse();
	}

	protected OutputStream getReponseOutputStream() throws IOException {
		return getFacesResponse().getOutputStream();
	}

	public String getArtistNameValue() {
		return artistNameValue;
	}

	public void setArtistNameValue(String artistNameValue) {
		this.artistNameValue = artistNameValue;
	}

	public ArtistCreditBean getArtistCreditBean() {
		return artistCreditBean;
	}

	public void setArtistCreditBean(ArtistCreditBean artistCreditBean) {
		this.artistCreditBean = artistCreditBean;
	}

	public ReleaseBean getReleaseBean() {
		return releaseBean;
	}

	public void setReleaseBean(ReleaseBean releaseBean) {
		this.releaseBean = releaseBean;
	}

	@Override
	public ArtistName get() {
		return artist;
	}

}
